package com.eqdd.yiqidian.ui.function.learn

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.LearnCommentCourseListBean
import com.eqdd.yiqidian.bean.rebuild.LearnLectureListBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.yiqixue.info.LectureInfoActivity
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_space.view.*
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by jhj on 18-9-25.
 */
class LearnCourseFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_space, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val status = arguments?.getInt(Config.STATUS)
        if (status == 0) {
            var pageNum = 0
            val mvcHelper = MVCCoolHelper<List<LearnCommentCourseListBean>>(view.coolRefreshView)
            val list = arrayListOf<LearnCommentCourseListBean>()
            view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
            view.recyclerView.layoutManager = LinearLayoutManager(activity)
            val slimAdapterEx = SlimAdapterEx.create()
                    .register<LearnCommentCourseListBean>(R.layout.list_item_193, SlimInjector<LearnCommentCourseListBean> { data, injector ->
                        data?.let {
                            injector?.text(R.id.tv_content, it.lectCourseTitle)
                                    ?.text(R.id.tv_time, it.lectureName)
                        }
                    })
                    .attachTo(view.recyclerView)
                    .updateData(list as List<*>?) as SlimAdapterEx<LearnCommentCourseListBean>?

            ItemClickSupport.addTo(view.recyclerView)
                    .setOnItemClickListener { recyclerView, position, v ->
                        val bean = slimAdapterEx?.getDataItem(position)
                        startActivity<LectureInfoActivity>(Config.ID to (bean?.id ?: 0))
                    }

            val dataSource = ModelRx2DataSource<LearnCommentCourseListBean>(ModelRx2DataSource.OnLoadSource<LearnCommentCourseListBean> { page, register ->
                if (page == 1) {
                    pageNum = 0
                }
                OkGo.post<HttpResult<List<LearnCommentCourseListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEARN_COURSE_RECOMMEND)
                        .params("page", pageNum)
                        .converter(object : JsonConverter<HttpResult<List<LearnCommentCourseListBean>>>() {
                            override fun test() {
                                super.test()
                            }
                        })
                        .adapt(FlowableBody<HttpResult<List<LearnCommentCourseListBean>>>())
                        .flatMap({ result ->
                            pageNum = result.page
                            Flowable.just(result.items ?: arrayListOf())
                        })
            })

            mvcHelper.setDataSource(dataSource)
            mvcHelper.adapter = slimAdapterEx
            mvcHelper.refresh()

        } else {
            var pageNum = 0
            val mvcHelper = MVCCoolHelper<List<LearnLectureListBean>>(view.coolRefreshView)
            val list = arrayListOf<LearnLectureListBean>()
            view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
            view.recyclerView.layoutManager = LinearLayoutManager(activity)
            val slimAdapterEx = SlimAdapterEx.create()
                    .register<LearnLectureListBean>(R.layout.list_item_193, SlimInjector<LearnLectureListBean> { data, injector ->
                        data?.let {
                            val time = it.createTime
                            injector?.text(R.id.tv_content, it.courseTheme)
                                    ?.text(R.id.tv_time,  it.lectureName)
                        }
                    })
                    .attachTo(view.recyclerView)
                    .updateData(list as List<*>?) as SlimAdapterEx<LearnLectureListBean>?

            ItemClickSupport.addTo(view.recyclerView)
                    .setOnItemClickListener { recyclerView, position, v ->
                        val bean = slimAdapterEx?.getDataItem(position)
                        startActivity<LectureInfoActivity>(Config.ID to (bean?.id ?: 0))
                    }

            val dataSource = ModelRx2DataSource<LearnLectureListBean>(ModelRx2DataSource.OnLoadSource<LearnLectureListBean> { page, register ->
                if (page == 1) {
                    pageNum = 0
                }
                OkGo.post<HttpResult<List<LearnLectureListBean>>>(HttpConfig.BASE_URL + HttpConfig.LEAEN_COURSE)
                        .params("page", pageNum)
                        .converter(object : JsonConverter<HttpResult<List<LearnLectureListBean>>>() {
                            override fun test() {
                                super.test()
                            }
                        })
                        .adapt(FlowableBody<HttpResult<List<LearnLectureListBean>>>())
                        .flatMap({ result ->
                            pageNum = result.page
                            Flowable.just(result.items ?: arrayListOf())
                        })
            })

            mvcHelper.setDataSource(dataSource)
            mvcHelper.adapter = slimAdapterEx
            mvcHelper.refresh()
        }
    }

}